package com.ruoyi.web.controller.system;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysClass;
import com.ruoyi.system.service.ISysClassService;

/**
 * 班级信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/class")
public class SysClassController extends BaseController
{
    @Autowired
    private ISysClassService classService;

    /**
     * 获取班级列表
     */
    @PreAuthorize("@ss.hasPermi('system:class:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysClass sysClass)
    {
        startPage();
        List<SysClass> list = classService.selectClassList(sysClass);
        return getDataTable(list);
    }

    /**
     * 根据班级编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:class:query')")
    @GetMapping(value = "/{classId}")
    public AjaxResult getInfo(@PathVariable Long classId)
    {
        return success(classService.selectClassById(classId));
    }

    /**
     * 新增班级
     */
    @PreAuthorize("@ss.hasPermi('system:class:add')")
    @Log(title = "班级管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysClass sysClass)
    {
        if (!classService.checkClassNameUnique(sysClass.getClassName()))
        {
            return error("新增班级'" + sysClass.getClassName() + "'失败，班级名称已存在");
        }
        sysClass.setCreateBy(getUsername());
        return toAjax(classService.insertClass(sysClass));
    }

    /**
     * 修改班级
     */
    @PreAuthorize("@ss.hasPermi('system:class:edit')")
    @Log(title = "班级管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysClass sysClass)
    {
        sysClass.setUpdateBy(getUsername());
        return toAjax(classService.updateClass(sysClass));
    }

    /**
     * 删除班级
     */
    @PreAuthorize("@ss.hasPermi('system:class:remove')")
    @Log(title = "班级管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{classIds}")
    public AjaxResult remove(@PathVariable Long[] classIds)
    {
        return toAjax(classService.deleteClassByIds(classIds));
    }

    /**
     * 获取班级选择框列表
     */
    @PreAuthorize("@ss.hasPermi('system:class:query')")
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        List<SysClass> classes = classService.selectClassAll();
        return success(classes);
    }
}